﻿namespace Randomness
{
    using mVids;
    using myLog;
    using System;
    using System.Collections.Generic;

    internal class smartRandom
    {
        private List<mVids.musicVideo> doPlayCount(List<mVids.musicVideo> allVids, int percentNoPlay)
        {
            List<mVids.musicVideo> list = new List<mVids.musicVideo>();
            int num = (allVids.Count * percentNoPlay) / 100;
            foreach (mVids.musicVideo video in allVids)
            {
                if ((int.Parse(video.playcount) == 0) && (list.Count < num))
                {
                    list.Add(video);
                }
            }
            return list;
        }

        private List<mVids.musicVideo> doRecent(List<mVids.musicVideo> allVideos, int percentRecent)
        {
            myLog.logger.log("avc" + allVideos.Count.ToString() + "pr" + percentRecent.ToString());
            myLog.logger.log(((allVideos.Count * percentRecent) / 100).ToString());
            int num = (allVideos.Count * percentRecent) / 100;
            myLog.logger.log("recentcount=" + num.ToString());
            List<mVids.musicVideo> list = new List<mVids.musicVideo>();
            foreach (mVids.musicVideo video in allVideos)
            {
                if (allVideos.IndexOf(video) < num)
                {
                    list.Add(video);
                }
            }
            return list;
        }

        public List<mVids.musicVideo> getSmartRandom(mVids.database db, int intNoPlay, int intRecent, int intTop, int percentRecent, int percentTop, int percentNoPlay, int totalVideosPercent)
        {
            List<mVids.musicVideo> allVideos = db.getFilesByKeyOrder(db.getFiles().Count);
            int num = (allVideos.Count * totalVideosPercent) / 100;
            myLog.logger.log("vidcount=" + allVideos.Count.ToString());
            List<mVids.musicVideo> list2 = this.doRecent(allVideos, percentRecent);
            List<mVids.musicVideo> list3 = this.doPlayCount(allVideos, percentNoPlay);
            int limit = (allVideos.Count * percentTop) / 100;
            List<mVids.musicVideo> list4 = db.getFilesByScore(limit);
            myLog.logger.log(limit.ToString());
            myLog.logger.log("recentVideos count=" + list2.Count.ToString() + " noplayvideos count=" + list3.Count.ToString() + " topvideos count=" + list4.Count.ToString());
            int num3 = (list2.Count + list3.Count) + list4.Count;
            int maxValue = (intNoPlay + intRecent) + intTop;
            intRecent = intNoPlay + intRecent;
            intTop = intRecent + intTop;
            Random random = new Random((int) DateTime.Now.Ticks);
            List<mVids.musicVideo> list5 = new List<mVids.musicVideo>();
            while (list5.Count <= num)
            {
                int num6;
                int num5 = random.Next(1, maxValue);
                if ((num5 <= intNoPlay) && (list3.Count > 0))
                {
                    num6 = random.Next(0, list3.Count - 1);
                    myLog.logger.log(string.Concat(new object[] { "[sRandom-noPlay adding ", list3[num6].Label, " from ", num6 }));
                    list5.Add(list3[num6]);
                    list3.RemoveAt(num6);
                }
                else if ((num5 <= intRecent) && (list2.Count > 0))
                {
                    num6 = random.Next(0, list2.Count - 1);
                    myLog.logger.log(string.Concat(new object[] { "[sRandom-recent adding ", list2[num6].Label, " from ", num6 }));
                    list5.Add(list2[num6]);
                    list2.RemoveAt(num6);
                }
                else if ((num5 <= intTop) && (list4.Count > 0))
                {
                    num6 = random.Next(0, list4.Count - 1);
                    myLog.logger.log(string.Concat(new object[] { "[sRandom-top adding ", list4[num6].Label, " from ", num6 }));
                    list5.Add(list4[num6]);
                    list4.RemoveAt(num6);
                }
                if (((list3.Count == 0) && (list2.Count == 0)) && (list4.Count == 0))
                {
                    break;
                }
            }
            myLog.logger.log("Added " + list5.Count.ToString() + " videos of chosen " + num3.ToString());
            myLog.logger.log("Total Weight " + maxValue);
            return list5;
        }
    }
}

